package org.imlinker.plugin;

import java.util.ArrayList;
import java.util.regex.Pattern;

import org.imlinker.event.Event;
import org.imlinker.event.EventListener;
import org.imlinker.plugin.say.AccountManager;
import org.imlinker.plugin.say.FanfouBot;
import org.imlinker.plugin.say.SayWindow;
import org.imlinker.plugin.say.WhoFollow;
import org.imlinker.util.StringUtils;

public class Say extends Plugin {

	public Say() {
		super("Say");

		editAccount(null, null);

		initChats();
	}

	public void editAccount(String newUsername, String newPassword) {
		String username = newUsername;
		String password = newPassword;
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			username = this.getUser();
			password = this.getPassword();
		}

		while (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			AccountManager.getAccountToSay(this, username, password);

			username = this.getUser();
			password = this.getPassword();
		}

		fanfouBot = new FanfouBot(username, password);
		if (!fanfouBot.initial()) {
			fanfouBot = null;
			System.out.println("Initfial fanfou failed");
		}

	}

	public void initChats() {
		ArrayList<String> buddyNames = getBuddyNamse();
		String regEx = "^fanfou\\d+@gmail.com$";
		Pattern fanfouPattern = Pattern
				.compile(regEx, Pattern.CASE_INSENSITIVE);

		for (String name : buddyNames) {
			if (fanfouPattern.matcher(name).matches()) {
				robots.add(name);
				this.getChatAdapter().addListener(name, new EventListener() {

					@Override
					public void eventRecieved(Event e) {
						// System.out.println("say recieve:" + e.getDescri());
						window.display(e.getDescri());
					}

				});

			}
		}
	}

	@Override
	public void open(boolean visible) {

		if (window == null) {
			window = new SayWindow(this);
		}

		window.setVisible(visible);
	}

	public void send(String m) {
		for (String name : robots) {
			this.getChatAdapter().sendMessage(name, m);
		}
	}

	public ArrayList<String> getRecommandList() {
		String user = this.getUser();
		if (StringUtils.isEmpty(user))
			return null;

		ArrayList<String> names = WhoFollow.recommandList("fanfou", user);
		ArrayList<String> friends = WhoFollow.getFriendList("fanfou", user);
		ArrayList<String> recommandList = new ArrayList<String>();
		if (names == null || friends == null) {
			return null;
		}

		int total = 10;
		for (String name : names) {
			if (friends.contains(name) || name.startsWith(user))
				continue;

			recommandList.add(name);

			total--;
			if (total == 0)
				break;
		}

		return recommandList;
	}

	public static void main(String args[]) {
		Say say = new Say();
		say.open(true);
	}

	public String getUser() {
		String user = this.getValue("user");
		return user;
	}

	public void setUser(String user) {
		this.setValueAt("user", user);
		this.save();
	}

	public String getPassword() {
		String password = this.getValue("password");
		return password;
	}

	public void setPassword(String newPassword) {
		this.setValueAt("password", newPassword);
		this.save();
	}

	public void addBuddy(String user) {
		if (fanfouBot != null) {
			fanfouBot.addBuddy(user);
		}

		System.out.println("say.addBuddy");
	}

	private FanfouBot fanfouBot;

	private SayWindow window = null;

	private ArrayList<String> robots = new ArrayList<String>();

}
